<?php
@$act = $_GET['act'];
@$id  = $_GET['id'];
@$id_type = 4;
$id_ref = 0;
@$td = $_POST['name'];
@$td_en = $_POST['name_en'];
@$name_hinh = $_FILES['img']['name'];
@$path_hinh = $_FILES['img']['tmp_name'];
@$mt = $_POST['des'];
@$mt_en = $_POST['des_en'];
@$nd = $_POST['cont'];
@$nd_en = $_POST['cont_en'];
$cur_date = date('Y-m-d');
@$del_img = $_POST['del_img'];
@$act = $_POST['txtEdit'];
@$id = $_POST['txtIdEdit'];
if(isset($_POST['btsave']))
{
	@$pic = $exec_mysql->execute("select img from node where id = $id",0);
	@$rowPic = mysql_fetch_array($pic);
	//-------Upload anh-------------
	//echo 'file name:'.$path_hinh;
	if($name_hinh != "")
	{
	
	$uploaddir = 'project/'; 
    $ext = explode('.',$name_hinh);
    if($ext[1]=='gif' || $ext[1]=='GIF'|| $ext[1]=='jpg' || $ext[1]=='JPG' || $ext[1]=='png' || $ext[1]=='PNG'){
        $id=rand();
        $filename=$id.'.'.$ext[1];
        $uploadfile = $uploaddir . $filename;
		$size=round(filesize($path_hinh)/1000,2);
		if($size < 3000)
		{
    
			 if (move_uploaded_file($path_hinh, $uploadfile)) { 
                
                
                //echo 'Link file ảnh vừa upload: <font color="#FF0000">'.$uploadfile.'</font></br>';
				echo '<i>(Dung lượng file ảnh đính kèm: '.$size.' Kb)</i>';
                @$path_hinh_u = $uploadfile;
            } 
            else 
			{ 
				$path_hinh_u = $rowPic['img'];
                echo "<script>showAlert('Lỗi','Có lỗi xảy ra khi upload ảnh!','');</script>"; 
            }
		}
		else
		{
			$path_hinh_u = $rowPic['img'];
			echo "Không thể upload được ảnh. File ảnh upload phải nhỏ hơn 3Mb!";
		}
	 
    }
    else 
	{
		$path_hinh_u = $rowPic['img'];
        echo "Không thể upload được ảnh. File ảnh không đúng chuẩn (jpg, jpeg, gif, png)!";
    } 
	
	}
	else
	{
		@$path_hinh_u = $rowPic['img'];
		//echo '<br> Path hinh:'.$path_hinh_u;
		
	}
	
	//---------end-------------
	$val = $exec_mysql->escape_string(array($id_type, $id_ref, $path_hinh_u, $td, $td_en, $mt, $mt_en, $nd, $nd_en, $cur_date));
	if(@$act == 'edit')
	{
		$saved = $exec_mysql->execute("update node set id_type = '$val[0]', id_ref = '$val[1]', img = '$val[2]', title_vi = '$val[3]', title_en = '$val[4]', description_vi = '$val[5]', description_en = '$val[6]', content_vi = '$val[7]', content_en = '$val[8]' where id = $id", 0);
		if($h <> $del_img)
		{
			unlink($del_img);
		}
		echo "<script>showAlert('Thông báo!', 'Cập nhật thông tin cho dự án thành công !', '');</script>";
	}
	else
	{
		$saved = $exec_mysql->execute("insert into node values ('','$val[0]','$val[1]','$val[2]','$val[3]', '$val[4]','$val[5]', '$val[6]','$val[7]', '$val[8]','$val[9]')",0);
		echo "<script>showAlert('Thông báo!', 'Đã thêm một dự án thành công !', '');</script>";
	}
}
?>
<?php
	/*Hàm phân trang*/
	$exec_mysql = new execute_mysql_query();
	$g_func = new global_function();
	@$cur_page  = str_replace('-','',$_GET['page']);
	$ROWS_PER_PAGE = 5;
	$total_record = $exec_mysql->execute("select id from node where id_type = 4", 2);
	$begin = $g_func->get_begin($cur_page, $ROWS_PER_PAGE, $total_record);
?>
<p class="group-line">
   <span class="group-title">Thông tin các dự án</span>
</p>
<table width="680px" class="table-list">
	<tr class="table-header">
        <td width="20px">STT</td>
        <td width="200px">Tiêu đề</td>
        <td>Tóm tắt nội dung</td>
        <td width="45px">Cập nhật</td>
        <td width="45px">Xóa</td>
    </tr>
    <?php
	@$g_func = new global_function();
	@$cur_page = $_GET['page'];
	$ROWS_PER_PAGE = 10;
	$total_record = $exec_mysql->execute("select id from node where id_type = 3", 2);
	$begin = $g_func->get_begin($cur_page, $ROWS_PER_PAGE, $total_record);
	$result = $exec_mysql->execute("select * from node where id_type = 4 order by id_type, id desc limit $begin, $ROWS_PER_PAGE",0);
	$stt = $begin + 1;
	while($row = mysql_fetch_array($result))
	{
		echo '<tr>';
			echo '<td align="center">'.$stt++.'</td>';
			echo '<td>'.$row['title_vi'].' / '.$row['title_en'].'</td>';
			echo '<td style="text-align:justify">'.cut($row['description_vi'],50).' / '.cut($row['description_en'],50).'</td>';
			echo '<td align="center"><a href="admin_master_page.php?cmd=4.3&id='.$row['id'].'&act=edit" ><img src="img/ico_edit.png" /></a></td>';
			//echo '<td align="center"><a href="admin_master_page.php?cmd=4.2&id='.$row['id'].'&act=del" onclick="return Del()"><img src="img/ico_delete.png" /></a></td>';
			echo '<td align="center"><img src="img/ico_delete.png" onclick="showConfirm(\'Xác nhận\', \'Bạn có chắc muốn xóa không?\', \'admin_master_page.php?cmd=4.2&act=del&id='.$row['id'].'\')" class="del" /></td>';
		echo '</tr>';
	}
	?> 
</table>
<script type="text/javascript">
function Del()
{
	var del=confirm("Bạn có chắc muốn xóa mẩu tin?");
    return del;
}
</script>
<?php
	@$id = $_GET['id'];
	@$act = $_GET['act'];
	if($act == 'del')
	{
		$hinh = $exec_mysql->execute("select img from node where id = $id",0);
		$row1 = mysql_fetch_array($hinh);
		unlink($row1['img']);
		$exec_mysql->execute("delete from node where id = $id",0);
		echo "<script>showAlert('Thông báo!', 'Đã xóa thành công !', '');</script>";
		echo "<script>";
			echo "window.location = 'admin_master_page.php?cmd=4.2'";
		echo "</script>";
	}
?>
<div class="phan_trang">
	<?php
		$g_func->admin_paging($total_record,$ROWS_PER_PAGE);
	?>
</div>

<?php
function cut($str, $len) {
    $str = trim($str);
    if (strlen($str) <= $len) return $str;
    $str = substr($str, 0, $len);
    if ($str != "") {
        if (!substr_count($str, " ")) return $str." ...";
        while (strlen($str) && ($str[strlen($str) - 1] != " ")) $str = substr($str, 0, -1);
        $str = substr($str, 0, -1)." ...";
    }
    return $str;
}	
?>















